package com.tiza.xiaoyoubao.ui.fragment;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tianze.library.utils.AppUtils;
import com.tiza.xiaoyoubao.MainActivity;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.AppContext;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.DateInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.ui.activity.SplashActivity;
import com.tiza.xiaoyoubao.utils.UpdateManger;

import org.simple.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;



public class LoginFragment extends BaseFragment {

    @BindView(R.id.tv_register)
    TextView tv_register;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_passWord)
    EditText et_passWord;
    @BindView(R.id.btn_Login)
    Button btn_Login;

    private String userName, passWord;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {
        userName = Hawk.get(Constants.KEY_USER_NAME, "");
        passWord = Hawk.get(Constants.KEY_USER_PASSWD, "");
    }

    @Override
    protected void initView() {
        setLeftActionText("退出");
        //注册设置下划线
        TextPaint textPaint = tv_register.getPaint();
        textPaint.setFlags(Paint.UNDERLINE_TEXT_FLAG);
        //抗锯齿
        textPaint.setAntiAlias(true);
        et_phone.setText(userName);
        et_passWord.setText(passWord);
        if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(passWord))
            btn_Login.performClick();
    }
    @OnClick({R.id.tv_register, R.id.tv_password, R.id.btn_Login})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_register:
                UIHelper.showRegister(mActivity);
                finish();
                break;
            case R.id.tv_password:
                UIHelper.showForgetPassWord(mActivity);
                break;
            case R.id.btn_Login:
                String message = checkLogin();
                if (TextUtils.isEmpty(message)) {
                doLogin();
                } else {
                    showWarnDialog(message);
                }
                break;
        }
    }
    private String checkLogin() {
        userName = et_phone.getText().toString().trim();
        if (!AppUtils.isMobile(userName))
            return "请输入正确的手机号";
        passWord = et_passWord.getText().toString().trim();
        if (passWord.length() == 0)
            return "请输入密码";
        return "";
    }
    private void doLogin() {
        ApiHttpClient.login(LoginFragment.this, userName, passWord,
                            new JsonCallback<UserInfo>(UserInfo.class) {

                                @Override
                                public void onBefore(Request request) {
                                    super.onBefore(request);
                                    showWaitingDialog();
                                }

                                @Override
                                public void onError(Call call, Exception e) {
                                    toast("登录失败");
                                }

                                @Override
                                public void onResponse(ResponseInfo<UserInfo> response) {
                                    int code = response.getCode();
                                    String message = response.getMessage();
                                    if (code == 1) {
                                        showWarnDialog(message);
                                        return;
                                    }
                                    List<UserInfo> data = response.getData();
                                    if (null == data || data.size() == 0) {
                                        toast("登录失败");
                                        return;
                                    }
                                    // 保存用户名,密码,用户信息
                                    Hawk.put(Constants.KEY_USER_NAME, userName);
                                    Hawk.put(Constants.KEY_USER_PASSWD, passWord);
                                    UserInfo _userInfo = data.get(0);
                                    System.out.println("手机号是"+_userInfo.getWebzzCmsBuyerInfo().getTelphone());
                                    Hawk.put(Constants.KEY_USER_INFO, _userInfo);
                                    // 修改登录状态
                                    AppContext.getInstance().setLogin(true);
                                    // 通知登录成功
                                    EventBus.getDefault().post(true, Constants.TAG_LOGIN_SUCCESS);
                                    finish();
                                    UIHelper.show(getActivity(), MainActivity.class, true);
                                }

                                @Override
                                public void onAfter() {
                                    super.onAfter();
                                    hideWaitingDialog();
                                }
                            }
        );
    }

}
